
package com.smartwork.common.utils.encrypt;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public final class MD5Util {
	private MD5Util() {
	}

	public static String encode32MD5(String str) {
		MessageDigest messageDigest = null;
		try {
			messageDigest = MessageDigest.getInstance("MD5");
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		}
		messageDigest.update(str.getBytes());
		byte[] strByte = messageDigest.digest();
		String mdStr = "";
		String tempStr = "";
		for (int n = 0; n < strByte.length; n++) {
			tempStr = Integer.toHexString(strByte[n] & 0XFF);
			if (tempStr.length() == 1) {
				mdStr = mdStr + 0 + tempStr;
			} else {
				mdStr = mdStr + tempStr;
			}
			if (n < strByte.length - 1) {
				mdStr = mdStr + "";
			}
		}
		return mdStr.toUpperCase();
	}

	public static String encode16MD5(String str) {
		String enStr = encode32MD5(str);
		enStr = enStr.substring(8, 24);
		return enStr;
	}


	public static void main(String[] arps) {
		System.out.println(MD5Util.encode32MD5("9d8a121ce581499d"));
		System.out.println(MD5Util.encode16MD5("9d8a121ce581499d"));

	}
}
